#!BPY

"""
Name: 'Videos_texture'
Blender: 242
Group: 'Misc'
Tooltip: ''
"""				
__author__ = "Alejandro Omar Chocano Vasquez"  
__url__ = ("http://alexvaqp.googlepages.com")
__version__ = "0.3"
__bpydoc__ = ""

import os
import platform

from Blender import *
from Blender import Texture,Image,Material,sys,Window,Scene
from Blender.Scene import Render
##from Blender.Scene.Render import *


class GLOBALES:
       pass

glob=GLOBALES()

glob.OS=platform.system()
glob.PathDir=os.getcwd() #get work dir

glob.VIDEO='video_path'
glob.Num_Frames=30
glob.Init_Frame=12
glob.FPS=15
glob.size_X=800
glob.size_Y=600

glob.scn=Scene.GetCurrent()

if glob.OS == 'Linux':
	glob.char_sep="/"
else:
	glob.char_sep="\\"


# Create video texture tex_name=Name of new texture   
def Create_texture(tex_name):
	  
	try:
		glob.tex = Texture.Get(tex_name)
	except NameError:
		glob.tex = Texture.New(tex_name)            # creating texture named 'tex'
		
	glob.tex.setType('Image')                 # make text be an image texture
	img = Image.Load(glob.VIDEO)              # load a movie
	img.setSpeed(glob.FPS)
	glob.tex.image = img                      # link the image to the texture
	glob.tex.animFrames=glob.Num_Frames
	glob.tex.animOffset=glob.Init_Frame       #set initial frame
	
 
#-----

# Set new texture in material 
def Set_material(mtl_name):
	
	try:
		mtl=Material.Get(mtl_name)    #get material clip
	except NameError: 		
		mtl=Material.New(mtl_name)             
		
	mtl.setTexture(0,glob.tex)            #place texture in clip
	

def readfile( file_name ):
	"Open file, read and return arreglo."
	f = open( file_name )
	renglones = f.readlines()
	arreglo = map( lambda x: x.split(','), renglones )
#	print arreglo
	return arreglo

### matrix  0:path_video 1:Frame_init 2:Frame_end 
### 3er r   0:FPS 1:size_X 2:size_Y

def Set_Clips():
		
	#read file
	matriz = readfile(glob.PathDir+glob.char_sep+'data1.txt')
		
	print matriz	

	glob.scn =Scene.Get(matriz[3][0])
	glob.scn.makeCurrent()
 	context = glob.scn.getRenderingContext()
	
	context.fps = int(matriz[2][0])
	context.imageSizeX(int(matriz[2][1]))
	context.imageSizeY(int(matriz[2][2]))
	##context.setImageType(matriz[2][3]) 
	##context.imageType=Render.PNG
	
	#Set video format	
	if matriz[2][3] == 'AVIRAW':
		context.setImageType(Render.AVIRAW)
		
	elif matriz[2][3] == 'AVIJPEG':
		context.setImageType(Render.AVIJPEG)	
		
	elif matriz[2][3] == 'AVICODEC':
		context.setImageType(Render.AVICODEC)	
		
	
	
	#------
	

	#for renglon in matriz:
	#glob.VIDEO=matriz[celda][0]
	
	# Get video A dates
	glob.VIDEO=matriz[0][0]
	glob.Init_Frame=int(matriz[0][1])
	glob.Num_Frames=int(matriz[0][2])
	##glob.FPS=int(matriz[0][3])
	##glob.size_X=int(matriz[0][4])
	##glob.size_Y=int(matriz[0][5])

	Create_texture('TexturaV1')
	Set_material('clipA')
	
	# Get video B dates
	glob.VIDEO=matriz[1][0]
	glob.Init_Frame=int(matriz[1][1])
	glob.Num_Frames=int(matriz[1][2])
	##glob.FPS=int(matriz[1][3])
	##glob.size_X=int(matriz[1][4])
	##glob.size_Y=int(matriz[1][5])

	Create_texture('TexturaV2')
	Set_material('clipB')
	
	
def main():
	
	#scn = Scene.GetCurrent()
 	
	Set_Clips()
	context = glob.scn.getRenderingContext()
	context.setRenderPath(glob.PathDir+glob.char_sep)
	context.renderAnim()	
	print glob.OS
	
main()	
